//
//  HWLOTRefreshFooterView.m
//  CLHTestCode
//
//  Created by 曹连华 on 2020/6/3.
//  Copyright © 2020 曹连华. All rights reserved.
//

#import "LOTRefreshFooterView.h"
#import <Lottie/LOTAnimationView.h>
#import <CoreCode/NSString+plus.h>
@interface LOTRefreshFooterView ()

@property (nonatomic, strong) LOTAnimationView *loding1;
@property (nonatomic, strong) LOTAnimationView *loding2;

@end

@implementation LOTRefreshFooterView

- (LOTAnimationView *)loding1 {
    if (!_loding1) {
        NSString *path = [NSString pathWithFileName:@"refresh_will_load_more" ofType:@"json" bundle:[NSBundle bundleForClass:[self class]] resourceBundleName:@"RefreshComponentResources.bundle"];
        _loding1 = [LOTAnimationView animationWithFilePath:path];
        _loding1.contentMode = UIViewContentModeScaleToFill;
        _loding1.hidden = NO;
    }
    return _loding1;
}

- (LOTAnimationView *)loding2 {
    if (!_loding2) {
        NSString *path = [NSString pathWithFileName:@"refresh_will_load_more" ofType:@"json" bundle:[NSBundle bundleForClass:[self class]] resourceBundleName:@"RefreshComponentResources.bundle"];
        _loding2 = [LOTAnimationView animationWithFilePath:path];
        _loding2.contentMode = UIViewContentModeScaleToFill;
        _loding2.hidden = YES;
        _loding2.loopAnimation = YES;
        [_loding2 play];
    }
    return _loding2;
}

- (void)placeSubviews {
    [super placeSubviews];
    [self addSubview:self.loding1];
    [self addSubview:self.loding2];
    self.loding1.frame = CGRectMake((self.mj_w - 35)/2, 0, 35, 35);
    self.loding2.frame = CGRectMake((self.mj_w - 35)/2, 0, 35, 35);
}
- (void)scrollViewContentOffsetDidChange:(nullable NSDictionary *)change {
    [super scrollViewContentOffsetDidChange:change];
    self.loding1.animationProgress = self.pullingPercent > 1 ? 1 : self.pullingPercent;
    if (self.pullingPercent <= 0.02) {
        self.loding1.hidden = YES;
        self.loding2.hidden = YES;
    }
}

- (void)scrollViewPanStateDidChange:(NSDictionary *)change {
    [super scrollViewPanStateDidChange:change];
    if (self.state == MJRefreshStateIdle) {
        self.loding1.hidden = NO;
        self.loding2.hidden = YES;
    } else if (self.state == MJRefreshStatePulling) {
        self.loding1.hidden = YES;
        self.loding2.hidden = NO;
    } else if (self.state == MJRefreshStateNoMoreData) {
        self.loding1.hidden = YES;
        self.loding2.hidden = YES;
    }
}

@end
